# __author__ = 'chenbinghui'
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import scrapy
from turorial.items import QuotesItem

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    #  简单的写法就是 start_urls = ['http://quotes.toscrape.com/page/1/']
    def start_requests(self):
        urls = [
            'http://quotes.toscrape.com/page/1/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        for quote in response.css('div.quote'):
            item = QuotesItem()
            item['title'] = quote.css('span.text::text').extract_first()
            item['author'] = quote.css('small.author::text').extract_first()
            item['tags'] = quote.css('div.tags a.tag::text').extract_first()
            print("item----title:%s-----author:%s-----tags:%s" % (item['title'],item['author'],item['tags']))
            yield item
        next_page = response.css('li.next a::attr(href)').extract_first()
        if next_page is not None:
            yield scrapy.Request(response.urljoin(next_page))
